package com.example.demo.common;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登陆拦截器,验证用户登陆状态
 * **/
@Component
public class LoginInterceptor implements HandlerInterceptor {
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //获取session,得到用户信息
       HttpSession session=request.getSession(false); //控制层那里是必须要获得session,这里是查看是否有session
       if(session!=null && session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null) {
           //用户已经登陆
           return true;
       }
       response.setStatus(401);
        return false;
   }
}
